package com.shouhuobao.take.parcel;

import java.util.ArrayList;

import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.ParcelBean;

import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

/**
 * 已到店的包裹列表<br>
 * Created by joker on 2017/5/25.
 */
public class Parcel1Fragment extends BaseParcelFragment {

	private TextView mParcelCount;
	@Override
	protected void findViewById() {
		setContentView(R.layout.take_parcel_list);
		super.findViewById();
		mAdapter = new ParcelAdapter(new ArrayList<ParcelBean>(), getActivityContext());
		mListView.setAdapter(mAdapter);

		View headerView = LayoutInflater.from(getActivityContext()).inflate(R.layout.take_parcel_header1, null);
		mParcelCount = (TextView) headerView.findViewById(R.id.parcel_count);
		// 先不做搜索功能
		mListView.addHeaderView(headerView);
		
		showLoadingDialog(null);
		super.parcelType = 1;
		AppHttp.getInstance().takeParcelQueryList(parcelType, 1, null, null);
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case MsgID.take_parcel_reachstore:
			final ReqResult<ParcelBean> result1 = super.handleMessage(msg.obj);
			getActivityContext().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mParcelCount.setText("当前共" + result1.getTotal() + "个包裹");
					mParcelCount.setVisibility(View.VISIBLE);
				}
			});
			return true;
		// 取走包裹
		case MsgID.take_parcel_away:
		case MsgID.take_parcel_check_bycheckcode:
			ReqResult<?> result = JSON.parser(msg.obj);
			if (result.isSuccess()) {
				getActivityContext().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mAdapter.getItems().remove(clickItem);
						mAdapter.notifyDataSetChanged();
						mParcelCount.setText("当前共" + mAdapter.getItemsSize() + "个包裹");
					}
				});
			} else {
				showToast(result.getMessage());
			}
			return false;
		default:
			return super.handleMessage(msg);
		}
	}

}
